<?php

namespace App\Models\Article;

use App\Models\BaseModel;
use App\Models\Traits\KfSoftDeletes;
use App\Models\Traits\KfUid;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\HasOne;

class ArticleUserRelationModel extends BaseModel
{
    use HasFactory;
    use KfUid;
    use KfSoftDeletes;

    protected $table = "article_user_relation";

    protected $fillable = [
        'id',
        'articleId',
        'type',
        'status',
        'isDeleted',
        'createdAt',
        'createdByUid',
        'updatedAt',
        'updatedByUid',
    ];

    const TYPE_LIKE    = 1;
    const TYPE_COLLECT = 2;
    const TYPE_SHARE   = 3;
    const TYPE_READ    = 4;
    const TYPE_SHOW    = 5;

    const TYPE_TEXT = [
        self::TYPE_LIKE    => "点赞",
        self::TYPE_COLLECT => "收藏",
        self::TYPE_SHARE   => "分享转发",
        self::TYPE_READ    => "阅读",
        self::TYPE_SHOW    => "展示",
    ];

    const STATUS_CANCEL  = 0;
    const STATUS_SUCCESS = 1;

    const STATUS_TEXT = [
        self::STATUS_CANCEL  => "取消",
        self::STATUS_SUCCESS => "正常",
    ];

    public static function add(array $data)
    {
        return self::query()->create($data);
    }

    public static function getInfo(int $articleId, int $userId, int $type)
    {
        return self::query()
                   ->where('articleId', $articleId)
                   ->where('createdByUid', $userId)
                   ->where('type', $type)
                   ->first();
    }

    public static function getType(int $logType): int
    {
        switch ($logType) {
            case ArticleStatisticsLogsModel::TYPE_LIKE:
            case ArticleStatisticsLogsModel::TYPE_CANCEL_LIKE:
                $type = self::TYPE_LIKE;
                break;
            case ArticleStatisticsLogsModel::TYPE_COLLECT:
            case ArticleStatisticsLogsModel::TYPE_CANCEL_COLLECT:
                $type = self::TYPE_COLLECT;
                break;
            case ArticleStatisticsLogsModel::TYPE_SHARE:
                $type = self::TYPE_SHARE;
                break;
            case ArticleStatisticsLogsModel::TYPE_READ:
                $type = self::TYPE_READ;
                break;
            case ArticleStatisticsLogsModel::TYPE_SHOW:
                $type = self::TYPE_SHOW;
                break;
            default:
                $type = 0;
        }
        return $type;
    }

    /**
     * 判断 内容是否已点赞
     * @param int $uid
     * @param int $articleId
     * @return bool
     */
    public static function checkIfSupport(int $uid, int $articleId): bool
    {
        $supportModel = self::getCount($articleId, $uid, self::TYPE_LIKE);
        if ($supportModel) {
            return true;
        }
        return false;
    }

    /**
     * 判断 内容是否已收藏
     * @param int $uid
     * @param int $articleId
     * @return bool
     */
    public static function checkIfCollect(int $uid, int $articleId): bool
    {
        $supportModel = self::getCount($articleId, $uid, self::TYPE_COLLECT);
        if ($supportModel) {
            return true;
        }
        return false;
    }


    public static function getCount(int $articleId, int $uid, int $type): int
    {
        return self::query()
                   ->where('articleId', $articleId)
                   ->where('createdByUid', $uid)
                   ->where('type', $type)
                   ->where('status', self::STATUS_SUCCESS)
                   ->count();
    }

    /**
     * @return HasOne
     */
    public function articleRel(): HasOne
    {
        return $this->hasOne(ArticleModel::class, "id", "articleId");
    }
}
